package com.zyh.controller;
import com.zyh.entity.Comment;
import com.zyh.resp.Result;
import com.zyh.service.CommentService;
import com.zyh.utils.GetNumber;
import com.zyh.utils.Menu;
import java.util.List;
import java.util.Scanner;

public class CommentController {
    private final CommentService commentService = new CommentService();
    private final Scanner scanner = new Scanner(System.in);
    private final GetNumber getNumber = new GetNumber();

    public void manageFromUser() {
        manage(true);
    }

    private void manage(boolean isFromUser) {
        while (true) {
            Menu.commentMenu();
            int opt = getNumber.getUserInputAsInt();
            switch (opt) {
                case 1:
                    selectById();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    likeById();
                    break;
                case 4:
                    selectAll();
                    break;
                case 5:
                    updateById();
                    break;
                case 0:
                    if (isFromUser) {
                        System.out.println("返回用户管理...");
                        return;
                    } else {
                        System.out.println("返回主菜单");
                        return;
                    }
                default:
                    System.out.println("无效选项，请重新选择");
            }
        }
    }

    private void selectById() {
        System.out.print("请输入评论ID：");
        Integer commentId = getNumber.getUserInputAsInt();
        try {
            Result<Comment> result = commentService.selectById(commentId);
            Comment comment = result.getData();
            System.out.println("查询结果：ID=" + comment.getCommentId() + "，内容=" + comment.getContent() + "，点赞数=" + comment.getLikeCount());
        } catch (Exception e) {
            System.out.println("查询失败：" + e.getMessage());
        }
    }

    private void add() {
        System.out.print("请输入用户ID：");
        Integer userId = getNumber.getUserInputAsInt();
        System.out.print("请输入评论内容：");
        String content = scanner.nextLine().trim();
        System.out.print("请输入父类型（post/news）：");
        String parentType = scanner.nextLine().trim();
        System.out.print("请输入父ID：");
        Integer parentId = getNumber.getUserInputAsInt();
        try {
            Comment comment = new Comment(userId, content, parentType, parentId);
            Result<Void> result = commentService.add(comment);
            System.out.println(result.getMessage());
        } catch (Exception e) {
            System.out.println("新增失败：" + e.getMessage());
        }
    }

    private void likeById() {
        System.out.print("请输入要点赞的评论ID：");
        Integer commentId = getNumber.getUserInputAsInt();
        try {
            Result<Void> result = commentService.likeById(commentId);
            System.out.println(result.getMessage());
        } catch (Exception e) {
            System.out.println("点赞失败：" + e.getMessage());
        }
    }

    private void selectAll() {
        try {
            Result<List<Comment>> result = commentService.getAll();
            System.out.println("=====所有评论（共" + result.getData().size() + "条）=====");
            result.getData().forEach(comment ->
                    System.out.println("ID=" + comment.getCommentId() + "，用户ID=" + comment.getUserId() + "，内容=" + comment.getContent() + "，点赞数=" + comment.getLikeCount())
            );
        } catch (Exception e) {
            System.out.println("查询失败：" + e.getMessage());
        }
    }

    private void updateById() {
        System.out.print("请输入要修改的评论ID：");
        Integer commentId = getNumber.getUserInputAsInt();
        System.out.print("请输入新的评论内容：");
        String newContent = scanner.nextLine().trim();
        try {
            Result<Void> result = commentService.updateById(commentId, newContent);
            System.out.println(result.getMessage());
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }
}
